//
//  FileView.m
//  FileView
//
//  Created by 甘文鹏 on 2018/4/24.
//  Copyright © 2018年 ganwenpeng.com. All rights reserved.
//

#import "FileView.h"
#import <Masonry/Masonry.h>

#define K_FontSize 12

@interface FileView()
@property (nonatomic, strong) CAShapeLayer *outerLayer;
@property (nonatomic, strong) CAShapeLayer *innerLayer;

@property (nonatomic, strong) UIColor *areaColor;
@property (nonatomic, strong) NSMutableArray *innerPointArray;
@property (nonatomic, strong) NSMutableArray *outerPointArray;
@property (nonatomic, assign) CGFloat angleOfBrokenLineToRectangleBottomPI;
@property (nonatomic, assign) CGFloat angleOfBrokenLineToTriangleLeftPI;

@property (nonatomic, weak) UILabel *fileNameLabel;
@property (nonatomic, weak) UILabel *fileSizeLabel;

@property (nonatomic, assign) CGPoint vertexPoint;
@end

@implementation FileView
#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    // 折角参数初始化
    {
        self.innerPointArray = [NSMutableArray array];
        self.outerPointArray = [NSMutableArray array];
        
        self.innerBgColor = [UIColor whiteColor];
        self.outerBgColor = [UIColor grayColor];
        
        self.areaColor = [UIColor whiteColor];
        
        self.borderWidth = 1;
        
        self.distanceOfBottomPointToRectangleRight = 50;
        self.angleOfBrokenLineToRectangleBottom = M_PI_4;
    }
    
    // label 参数初始化
    {
        self.fileNameFont = [UIFont systemFontOfSize:K_FontSize];
        self.fileNameTextColor = [UIColor blackColor];
        
        self.fileSizeFont = [UIFont systemFontOfSize:K_FontSize];
        self.fileSizeTextColor = [UIColor grayColor];
    }
    
    // 文件大小
    {
        UILabel *fileSizeLabel = [[UILabel alloc] init];
        self.fileSizeLabel = fileSizeLabel;
        [self addSubview:fileSizeLabel];
        
    }
    
    // 文件名
    {
        UILabel *fileNameLabel = [[UILabel alloc] init];
        self.fileNameLabel = fileNameLabel;
        [self addSubview:fileNameLabel];
    }
    
    // 内容边距初始化
    {
        self.contentInsets = UIEdgeInsetsMake(8, 8, 8, 8);
    }
    
    [self reLayoutLabels];
}

- (void)drawRect:(CGRect)rect {
    [self.innerLayer removeFromSuperlayer];
    [self.outerLayer removeFromSuperlayer];
    
    [self prepareDrawRect:rect];
    
    CAShapeLayer *outerLayer;
    CAShapeLayer *innerLayer;
    
    // outer
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointZero];
        
        for (id obj in self.outerPointArray) {
            CGPoint point = [obj CGPointValue];
            [path addLineToPoint:point];
        }
        
        
        outerLayer = [CAShapeLayer layer];
        outerLayer.path = path.CGPath;
        outerLayer.fillColor = self.outerBgColor.CGColor;
        self.outerLayer = outerLayer;
        [self.layer insertSublayer:outerLayer atIndex:0];
    }
    
    // inner
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(self.borderWidth, self.borderWidth)];
        
        for (id obj in self.innerPointArray) {
            CGPoint point = [obj CGPointValue];
            [path addLineToPoint:point];
        }
        
        
        innerLayer = [CAShapeLayer layer];
        innerLayer.path = path.CGPath;
        innerLayer.fillColor = self.innerBgColor.CGColor;
        self.innerLayer = innerLayer;
        [outerLayer addSublayer:innerLayer];
    }
}

- (void)prepareDrawRect:(CGRect)rect {
    CGSize size = rect.size;
    CGFloat half = MIN(size.width, size.height)/2;
    
    // 限制右下角折线与矩形构成的三角形两条直角边长度不超过矩形短边长度的一半
    {
        if (self.distanceOfBottomPointToRectangleRight > half) {
            self.distanceOfBottomPointToRectangleRight = half;
        }
        CGFloat right = self.distanceOfBottomPointToRectangleRight * tan(self.angleOfBrokenLineToRectangleBottomPI);
        if (right > half) {
            right = half;
            self.distanceOfBottomPointToRectangleRight = right / tan(self.angleOfBrokenLineToRectangleBottomPI);
        }
    }
    
    // 限制折角三角形的左侧边长度补偿过矩形短边的长度
    {
        if (self.lengthOfLeftForTriangle > half*2 - self.borderWidth) {
            self.lengthOfLeftForTriangle = half*2 - self.borderWidth;
        }
    }
    
    // outer
    {
        [self.outerPointArray removeAllObjects];
        [self.outerPointArray addObject:@(CGPointMake(0, size.height))];
        [self.outerPointArray addObject:@(CGPointMake(size.width - _distanceOfBottomPointToRectangleRight, size.height))];
        CGFloat h = tan(self.angleOfBrokenLineToRectangleBottomPI) * self.distanceOfBottomPointToRectangleRight;
        [self.outerPointArray addObject:@(CGPointMake(size.width, size.height - h))];
        [self.outerPointArray addObject:@(CGPointMake(size.width, 0))];
        
        [self.outerPointArray addObject:@(CGPointZero)];
    }
    
    // inner
    {
        [self.innerPointArray removeAllObjects];
        
        [self.innerPointArray addObject:@(CGPointMake(self.borderWidth, size.height - self.borderWidth))];
        [self.innerPointArray addObject:@(CGPointMake(size.width - _distanceOfBottomPointToRectangleRight, size.height - self.borderWidth))];
        
        // 顶点的相对坐标
        CGFloat vertexX = cos(self.angleOfBrokenLineToRectangleBottomPI + self.angleOfBrokenLineToTriangleLeftPI) * self.lengthOfLeftForTriangle;
        CGFloat vertexY = sin(self.angleOfBrokenLineToRectangleBottomPI + self.angleOfBrokenLineToTriangleLeftPI) * self.lengthOfLeftForTriangle;
        
        // 顶点的绝对坐标
        vertexX = (size.width - self.distanceOfBottomPointToRectangleRight) + vertexX;
        vertexX = MIN(size.width - self.borderWidth, vertexX);
        vertexY = size.height - vertexY;
        vertexY = MIN(vertexY, size.height - self.borderWidth);
        self.vertexPoint = CGPointMake(vertexX, vertexY);
        [self.innerPointArray addObject:@(self.vertexPoint)];
        [self.innerPointArray addObject:@(CGPointMake(size.width - self.borderWidth, size.height - tan(self.angleOfBrokenLineToRectangleBottomPI) * self.distanceOfBottomPointToRectangleRight))];
        [self.innerPointArray addObject:@(CGPointMake(size.width - self.borderWidth, self.borderWidth))];
        [self.innerPointArray addObject:@(CGPointMake(self.borderWidth, self.borderWidth))];
    }
}

- (void)setVertexPoint:(CGPoint)vertexPoint {
    _vertexPoint = vertexPoint;
    
    [self reLayoutLabels];
}

- (void)setContentInsets:(UIEdgeInsets)contentInsets {
    // 要保证四边间距大于 0
    _contentInsets = UIEdgeInsetsMake(MAX(0, contentInsets.top), MAX(0, contentInsets.left), MAX(0, contentInsets.bottom), MAX(0, contentInsets.right));
    
    [self reLayoutLabels];
}


/**
 重新对两个Label进行布局
 */
- (void)reLayoutLabels {
    
    CGFloat distanceR = (self.frame.size.width - self.vertexPoint.x) + self.contentInsets.right;
    if (CGPointEqualToPoint(self.vertexPoint, CGPointZero)) {
        distanceR = self.contentInsets.right;
    }
    
    CGSize size = [@"MB" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.fileSizeFont} context:nil].size;
    
    // 先布局fileSize，因为fileName的布局需要依赖他
    [self.fileSizeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(self.contentInsets.left);
        make.bottom.equalTo(self).with.offset(-self.contentInsets.bottom);
        make.right.equalTo(self).with.offset(-distanceR);
        make.height.mas_equalTo(size.height);
    }];
    
    [self.fileNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(self.contentInsets.left);
        make.top.equalTo(self).with.offset(self.contentInsets.top);
        make.right.equalTo(self).with.offset(-distanceR);
        
        // 文件名和文件大小的最小间距为 8 ，暂时不对外暴露修改接口
        make.bottom.lessThanOrEqualTo(self.fileSizeLabel.mas_top).with.offset(-8);
    }];
}

#pragma mark triangle
- (void)setDistanceOfBottomPointToRectangleRight:(CGFloat)distanceOfBottomPointToRectangleRight {
    _distanceOfBottomPointToRectangleRight = distanceOfBottomPointToRectangleRight;
    
    // 如果【三角形左侧边长】为0，将【三角形左侧边长】设置为【矩形底部折点到矩形右侧边的距离】，否则刷新组件内容。
    if (self.lengthOfLeftForTriangle == 0) {
        self.lengthOfLeftForTriangle = distanceOfBottomPointToRectangleRight;
    }
    
    [self setNeedsDisplay];
}

- (void)setAngleOfBrokenLineToRectangleBottom:(CGFloat)angleOfBrokenLineToRectangleBottom {
    // 取值范围 0~90
    angleOfBrokenLineToRectangleBottom = MIN(90, MAX(0, angleOfBrokenLineToRectangleBottom));
    
    _angleOfBrokenLineToRectangleBottom = angleOfBrokenLineToRectangleBottom;
    _angleOfBrokenLineToRectangleBottomPI = angleOfBrokenLineToRectangleBottom / 180 * M_PI;
    
    // 如果【折线与三角形左侧边夹角】为0，将【折线与三角形左侧边夹角】设置为【折线与矩形底边夹角】，否则刷新组件内容
    if (self.angleOfBrokenLineToTriangleLeft == 0) {
        self.angleOfBrokenLineToTriangleLeft = angleOfBrokenLineToRectangleBottom;
    }
    
    [self setNeedsDisplay];
}

- (void)setAngleOfBrokenLineToTriangleLeft:(CGFloat)angleOfBrokenLineToTriangleLeft {
    // 取值范围 0~90
    angleOfBrokenLineToTriangleLeft = MIN(90, MAX(0, angleOfBrokenLineToTriangleLeft));
    
    _angleOfBrokenLineToTriangleLeft = angleOfBrokenLineToTriangleLeft;
    _angleOfBrokenLineToTriangleLeftPI = angleOfBrokenLineToTriangleLeft / 180 * M_PI;
    
    [self setNeedsDisplay];
}

- (void)setLengthOfLeftForTriangle:(CGFloat)lengthOfLeftForTriangle {
    _lengthOfLeftForTriangle = lengthOfLeftForTriangle;
    
    [self setNeedsDisplay];
}

#pragma mark backgroundColor 、tintColor
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:[UIColor clearColor]];
}

- (void)setInnerBgColor:(UIColor *)innerBgColor {
    _innerBgColor = innerBgColor;
    
    [self setNeedsDisplay];
}

- (void)setOuterBgColor:(UIColor *)outerBgColor {
    _outerBgColor = outerBgColor;
    
    [self setNeedsDisplay];
}

#pragma mark border
- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    
    [self setNeedsDisplay];
}

#pragma mark filename
- (void)setFileNameText:(NSString *)fileNameText {
    _fileNameText = fileNameText;
    
    self.fileNameLabel.text = fileNameText;
}

- (void)setFileNameTextColor:(UIColor *)fileNameTextColor {
    _fileNameTextColor = fileNameTextColor;
    
    self.fileNameLabel.textColor = fileNameTextColor;
}

- (void)setFileNameFont:(UIFont *)fileNameFont {
    if (!fileNameFont) {
        fileNameFont = [UIFont systemFontOfSize:K_FontSize];
    }
    
    _fileNameFont = fileNameFont;
    
    self.fileNameLabel.font = fileNameFont;
}

- (void)setMaxLinesOfFileName:(NSInteger)maxLinesOfFileName {
    _maxLinesOfFileName = maxLinesOfFileName;
    
    self.fileNameLabel.numberOfLines = maxLinesOfFileName;
}

#pragma mark fileSize
- (void)setFileSizeText:(NSString *)fileSizeText {
    _fileSizeText = fileSizeText;
    
    self.fileSizeLabel.text = fileSizeText;
}

- (void)setFileSizeFont:(UIFont *)fileSizeFont {
    if (!fileSizeFont) {
        fileSizeFont = [UIFont systemFontOfSize:K_FontSize];
    }
    
    _fileSizeFont = fileSizeFont;
    
    self.fileSizeLabel.font = fileSizeFont;
}

- (void)setFileSizeTextColor:(UIColor *)fileSizeTextColor {
    _fileSizeTextColor = fileSizeTextColor;
    
    self.fileSizeLabel.textColor = fileSizeTextColor;
}
@end
